#include "precompile.h"

#include "ControlBars.h"

#include "form/IControl.h"
#include "form/IMenuBar.h"
#include "form/IToolBar.h"
#include "FreeIdentifier.h"

String ControlBars::ToolboxName = L"_Toolbox";

ControlBars::ControlBars()
{
	VRVERIFY(m_toolbox.CreateObject(CLSID_ToolBar));
	VRVERIFY(m_menuBar.CreateObject(CLSID_MenuBar));
}

ControlBars::~ControlBars()
{
}

IMenuBar* ControlBars::GetMenuBar() const
{
	sys::InterfacePtr<IMenuBar> pIFMenuBar(m_menuBar);
	pIFMenuBar->SetControlbars((IControlBars*)this);

	return pIFMenuBar;
}

IToolBar* ControlBars::GetToolbox() const
{
	sys::InterfacePtr<IToolBar> pIFToolbox(m_toolbox);
	pIFToolbox->SetName(ToolboxName);
	pIFToolbox->SetControlbars((IControlBars*)this);
	return pIFToolbox;
}

IToolBar* ControlBars::AddToolbar(const String& name,int32 align)
{
	ToolBarDictionary::const_iterator itfind = m_controlbars.find(ToolboxName);
	if (itfind != m_controlbars.end())
	{
		const sys::ObjectPtr& objbar = itfind->second;
		return sys::InterfacePtr<IToolBar>(objbar);
	}
	IToolBar* pIFFindedToolbar = FindToolbar(name);
	if (pIFFindedToolbar)
		return pIFFindedToolbar;
	else
	{
		sys::ObjectPtr objbar;
		objbar.CreateObject(CLSID_ToolBar);
		if (objbar.IsNotNull())
		{
			m_controlbars.insert(std::make_pair(name,objbar));
			sys::InterfacePtr<IToolBar> pIFToolbar(objbar);
			pIFToolbar->SetAlign((IToolBar::kToolBarAlign)align);
			pIFToolbar->SetName(name);
			pIFToolbar->SetControlbars(this);
			return pIFToolbar;
		}
	}
	return NULL;
}

IToolBar* ControlBars::FindToolbar(const String& name)
{
	ToolBarDictionary::const_iterator itfind = m_controlbars.find(name);
	if (itfind != m_controlbars.end())
	{
		const sys::ObjectPtr& objbar = itfind->second;
		return sys::InterfacePtr<IToolBar>(objbar);
	}
	return NULL;
}

uint32 ControlBars::GetToolbars(ToolbarList& toolbars) const
{
	ToolBarDictionary::const_iterator itbegin = m_controlbars.begin();
	ToolBarDictionary::const_iterator itend = m_controlbars.end();
	for (; itbegin != itend; ++itbegin)
	{
		toolbars.push_back(itbegin->second.P());
	}
	toolbars.push_back(m_toolbox.P());
	return toolbars.size();
}

bool ControlBars::ContainControl(uint32 id) const
{
	return m_mapCommands.find(id)!=m_mapCommands.end();
}

IObject* ControlBars::FindControl(uint32 id) const
{
	CommandMap::const_iterator itfind = m_mapCommands.find(id);
	if (itfind != m_mapCommands.end())
		return itfind->second;

	return NULL;
}

//////////////////////////////////////////////////////////////////////////
//protected methods
bool ControlBars::RegisterControl(IObject* control)
{
	FreeIdentifier<IObject*> identifier(ID_RESERVEID_FIRST,ID_RESERVEID_LAST,m_mapCommands);
	uint32 nGlobalID = identifier.GetUnique(control);
	if (nGlobalID == ERROR_ID)
		return false;

	sys::InterfacePtr<IControl> pIFControl(control);
	pIFControl->SetCommandID(nGlobalID);
	m_mapCommands[nGlobalID] = control;
	return true;
}
